package com.own.business.system.menu.controller.manage;

import com.own.aop.permission.PermissionModule;
import com.own.aop.permission.PermissionOperation;
import com.own.business.system.menu.business.entity.form.SystemMenuAdvancedSortForm;
import com.own.business.system.menu.business.service.SystemMenuService;
import com.own.business.system.menu.common.constant.ConstantModule;
import com.own.business.system.menu.entity.dto.SystemMenuDto;
import com.own.business.system.menu.entity.map.SystemMenuMapVo;
import com.own.business.system.menu.entity.query.SystemMenuQuery;
import com.own.business.system.menu.entity.vo.SystemMenuVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.common.util.list.TreeNode;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import com.own.constant.ConstantPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ManageSystemMenuController
 *
 * @author chenxueli
 * @date 2024-03-31 09:22:58
 */
@Tag(name = "系统菜单" + ConstantBeanSuffix.MANAGE_CLIENT + ConstantBeanSuffix.CONTROLLER)
@PermissionModule(ConstantModule.MENU)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.MANAGE_CLIENT + "system/menu")
public class ManageSystemMenuController extends BaseController {

    @Resource
    private SystemMenuService systemMenuService;

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "分页查询信息")
    @PostMapping("/page")
    public ResultModel<PageModel<SystemMenuVo>> page(
            @RequestBody SystemMenuQuery query
    ) {
        var pageModel = systemMenuService.page(query);
        return R.success(pageModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "查询菜单树信息")
    @PostMapping("/tree")
    public ResultModel<List<TreeNode<SystemMenuMapVo>>> tree(
            @RequestBody SystemMenuQuery query
    ) {
        var menuList = systemMenuService.map(query);
        var menuTreeList = menuList.stream().map(TreeNode::new).toList();
        var treeList = TreeNode.build(menuTreeList, SystemMenuMapVo::getId, SystemMenuMapVo::getParentId);
        return R.success(treeList);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.ADD)
    @Operation(summary = "添加信息")
    @PostMapping("/")
    public ResultModel<SystemMenuVo> add(
            @Validated @RequestBody SystemMenuDto dto
    ) {
        dto.check();
        var updateModel = systemMenuService.add(dto.toBo());
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.UPDATE)
    @Operation(summary = "修改信息")
    @PutMapping("/{menuId}")
    public ResultModel<SystemMenuVo> update(
            @Parameter(description = "主键id", required = true) @PathVariable Long menuId,
            @Validated @RequestBody SystemMenuDto dto
    ) {
        dto.checkUpdate(menuId);
        var updateModel = systemMenuService.update(menuId, dto.toBo());
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.DELETE)
    @ConfirmAop(message = "确认删除该项目？（注意：删除之后无法找回数据，且该项目所有资料会被删除）")
    @Operation(summary = "删除信息")
    @DeleteMapping("/{menuId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "主键id", required = true) @PathVariable Long menuId
    ) {
        var updateModel = systemMenuService.delete(menuId);
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.VIEW)
    @Operation(summary = "根据id获取信息")
    @GetMapping("/{menuId}")
    public ResultModel<SystemMenuVo> getById(
            @Parameter(description = "主键id", required = true) @PathVariable Long menuId
    ) {
        var vo = systemMenuService.getById(menuId);
        return R.success(vo);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.UPDATE)
    @Operation(summary = "修改菜单的禁用状态")
    @PutMapping("/disable/{menuId}/{disable}")
    public ResultModel<Boolean> updateDisable(
            @Parameter(description = "主键id", required = true) @PathVariable Long menuId,
            @Parameter(description = "禁用状态 0-启用 1-禁用", required = true) @PathVariable Integer disable
    ) {
        systemMenuService.updateDisableById(menuId, disable);
        return R.success();
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.UPDATE)
    @Operation(summary = "修改菜单的显示状态")
    @PutMapping("/show/{menuId}/{show}")
    public ResultModel<Boolean> updateShow(
            @Parameter(description = "主键id", required = true) @PathVariable Long menuId,
            @Parameter(description = "显示状态 0-显示 1-隐藏", required = true) @PathVariable Integer show
    ) {
        systemMenuService.updateShowById(menuId, show);
        return R.success();
    }

    @LogOperationAop
    @PermissionOperation("高级排序")
    @Operation(summary = "修改菜单的显示状态")
    @PutMapping("/advanced/sort")
    public ResultModel<Boolean> sort(
            @RequestBody SystemMenuAdvancedSortForm form
    ) {
        form.check();
        systemMenuService.sort(form);
        return R.success();
    }

}
